<?php
Zend_Loader::loadClass('Application_Form');
Zend_Loader::loadClass('Application_Form_Element_MaterialControls');

class Application_Form_Materiali extends Application_Form
{
	protected $_formDecorators = array(
			'FormElements',
			'Form',
			array('HtmlTag', array('tag' => 'div', 'id' => 'form-wrap')),
	);

	protected $_displaygroupDecorators = array(
			'FormElements',
			'Fieldset',
	);

	protected $_elementDecorators = array(
			'viewHelper',
			array(array('htmltag1' => 'HtmlTag'), array('tag' => 'div', 'class' => 'form-span')),
			array('Label', array('tag' => 'div', 'tagClass' => 'form-span'))
	);

	public function __construct() {
		parent::__construct();
		$this->addJavascript("/validator/jquery.validationEngine.js");
		$this->addJavascript("/validator/languages/jquery.validationEngine-en.js");
		$this->addJavascript("/dataTables/jquery.dataTables.min.js");
		$this->addStylesheet("/validator/validationEngine.jquery.css");
		$this->addStylesheet("/dataTables/jquery.dataTables.css");
		$this->addStylesheet("/dataTables/demo_table_jui.css");
		$this->addStylesheet("/dataTables/fix.css");
	}

	public function run() {
		$request = Zend_Controller_Front::getInstance()->getRequest();
		if (!$request->isXmlHttpRequest()) {
			// Contesto non-AJAX
			$this->_initForm();
			$this->_view->browserForm = $this->_form->getSubForm("browser")->render($this->_view);
			$this->_view->detailForm = $this->_form->getSubForm("detail")->render($this->_view);
			return;
			
		} else {
			// Contesto AJAX
			$layout = Zend_Layout::getMvcInstance();
			$layout->disableLayout();

			$ret = "";
			if ($request->isPost()) {
				// POST data
				foreach ($request->getParams() as $paramName => $paramValue) {
					$urlParams[$paramName] = urlencode($paramValue);
				}
				$this->_initForm();
				if ($this->_form->getSubForm("detail")->isValid($request->getParams())) {
					$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
					$redirector->setGotoRoute($urlParams, 'ajax');
					$redirector->redirectAndExit();
				} else {
					$messages = $this->_form->getSubForm("detail")->getMessages();
					if (array_key_exists('material_name', $messages)) {
						array_unshift($messages['material_name'], "Corregere gli errori segnalati");  
					} else {
						$messages['material_name'] = array ("generic" => "Corregere gli errori segnalati");
					}
					$ret = array("esito" => -1, "id" => $request->getParam('id'), "messages" => $messages);
				}
			} else {
				// Servizi ausiliari form
				switch ($request->getParam('function')) {
					default:
					$ret = null;
				}
			}
			echo Zend_Json::encode($ret);
		} 
	}

	protected function _initForm()
	{
		$browserForm = new Zend_Form(array("id" => "browser"));
		$detailForm = new Zend_Form(array("id" => "detail"));
		$this->_form->addSubForm($browserForm, "browser");
		$this->_form->addSubForm($detailForm, "detail");

		///// TOP (dataTables) /////

		Zend_Loader::loadClass('Materials');
		$materiali = new Materials();
		$recordSet = $materiali->select()->from($materiali, array("id", "name"))
		->where("status > ?", 0)
		->order("name");
		$this->_view->materials = $materiali->fetchAll($recordSet)->toArray();

		///// BOTTOM (zend form) /////

		$campo1 = new Zend_Form_Element_Hidden('material_order');
		$campo1->setValue(10); //($value)  setOptions(array("value" => 10));

		$campo2 = new Zend_Form_Element_Text('material_name');
		$campo2->setLabel('Nome del materiale')
		->setOptions(array(
			"id" => "material_name", 
			"order" => 3, 
			"size" => 60,
			"class" => "validate[required]"
		))
		->setRequired(true);

		$campo3 = new Application_Form_Element_MaterialControls("construction_controls");
		$campo3->setOptions(array(
			"id" => "construction_controls", 
			"order" => 2
		));

		$campo4 = new Zend_Form_Element_Text('material_descr');
		$campo4->setLabel('Descrizione')
		->setOptions(array(
			"id" => "material_descr", 
			"order" => 4, 
			"size" => 60
		));

		$campo5 = new Zend_Form_Element_Text('material_category');
		$campo5->setLabel('Categoria')
		->setOptions(array(
			"id" => "material_category", 
			"order" => 5, 
			"size" => 60
		));

		$campo7 = new Zend_Form_Element_Hidden('material_category_id');
		$campo7->setOptions(array(
				"id" => "material_category_id",
		));
		
		$campo6 = new Zend_Form_Element_Text('default_thickness');
		$campo6->setLabel('Spessore di default (m)')
		->setOptions(array(
			"id" => "default_thickness",
			"order" => 6,
			"size" => 60,
			"class" => "validate[required,custom[number]]"
		))
		->setRequired(true)
		->addValidator("Float", false, array("locale" => "en"));
		//$float = new Zend_Validate_Float();
		//$float->setLocale("en");
		//$campo6->addValidator($float);
		

		$campo100 = new Zend_Form_Element_Text('material_conductivity');
		$campo100->setLabel('Conducibilità (W/m K)')
		->setOptions(array(
			"id" => "material_conductivity", 
			"order" => 100,
			"size" => 60,
			"class" => "validate[required,custom[number]]"
		))
		->setRequired(true)
		->addValidator("Float", false, array("locale" => "en"));

		$campo101 = new Zend_Form_Element_Text('material_capacity');
		$campo101->setLabel('Calore specifico (J/Kg K)')
		->setOptions(array(
			"id" => "material_capacity", 
			"order" => 101,
			"size" => 60,
			"class" => "validate[required,custom[number]]"
		))
		->setRequired(true)
		->addValidator("Float", false, array("locale" => "en"));

		$campo102 = new Zend_Form_Element_Text('material_density');
		$campo102->setLabel('Densità (Kg/mc)')
		->setOptions(array(
			"id" => "material_density", 
			"order" => 102,
			"size" => 60,
			"class" => "validate[required,custom[number]]"
		))
		->setRequired(true)
		->addValidator("Float", false, array("locale" => "en"));

		$campo103 = new Zend_Form_Element_Text('material_r_value');
		$campo103->setLabel('Resistenza (valore-R)')
		->setOptions(array(
			"id" => "material_r_value", 
			"order" => 103, 
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));

		$campo104 = new Zend_Form_Element_Text('material_thermal_absorptance');
		$campo104->setLabel('Assorbenza termica (emissività)')
		->setOptions(array(
			"id" => "material_thermal_absorptance", 
			"order" => 104, 
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));

		$campo105 = new Zend_Form_Element_Text('material_solar_absorptance');
		$campo105->setLabel('Assorberza solare ')
		->setOptions(array(
			"id" => "material_solar_absorptance", 
			"order" => 105, 
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));

		$campo106 = new Zend_Form_Element_Text('material_visible_absorptance');
		$campo106->setLabel('Assorbenza visibile')
		->setOptions(array(
			"id" => "material_visible_absorptance", 
			"order" => 106, 
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));

		$campo107 = new Zend_Form_Element_Text('material_roughness');
		$campo107->setLabel('Rugosità')
		->setOptions(array(
			"id" => "material_roughness", 
			"order" => 107, 
			"size" => 60,
			"class" => "validate[required]",
		))
		->setRequired(true);

		$campo109 = new Zend_Form_Element_Text('material_co2');
		$campo109->setLabel('Embodied Carbon')
		->setOptions(array(
			"id" => "material_co2", 
			"order" => 109, 
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));

		$campo110 = new Zend_Form_Element_Text('material_conv_factor_name');
		$campo110->setLabel('Conversion factor name')
		->setOptions(array(
			"id" => "material_conv_factor_name", 
			"order" => 110, 
			"size" => 60
		));

		$campo111 = new Zend_Form_Element_Text('material_ecarbon_source');
		$campo111->setLabel('Data source/Factor Name')
		->setOptions(array(
			"id" => "material_ecarbon_source",
			"order" => 111,
			"size" => 60
		));

		$campo112 = new Zend_Form_Element_Text('material_assumption');
		$campo112->setLabel('Assumption /Factor boundary')
		->setOptions(array(
			"id" => "material_assumption",
			"order" => 112,
			"size" => 60
		));

		$campo114 = new Zend_Form_Element_Text('material_co2e');
		$campo114->setLabel('Embodied Carbon')
		->setOptions(array(
			"id" => "material_co2e", 
			"order" => 114, 
			"size" => 60,
			"class" => "validate[custom[number]]"
		))
		->addValidator("Float", false, array("locale" => "en"));
		
		// Adding form elements to form
		$this->_form->getSubForm("detail")->addElement($campo1);
		$this->_form->getSubForm("detail")->addElement($campo3);
		$this->_form->getSubForm("detail")->addElement($campo2);
		$this->_form->getSubForm("detail")->addElement($campo4);
		$this->_form->getSubForm("detail")->addElement($campo5);
		$this->_form->getSubForm("detail")->addElement($campo6);
		$this->_form->getSubForm("detail")->addElement($campo7);
		$this->_form->getSubForm("detail")->addElement($campo100);
		$this->_form->getSubForm("detail")->addElement($campo101);
		$this->_form->getSubForm("detail")->addElement($campo102);
		$this->_form->getSubForm("detail")->addElement($campo103);
		$this->_form->getSubForm("detail")->addElement($campo104);
		$this->_form->getSubForm("detail")->addElement($campo105);
		$this->_form->getSubForm("detail")->addElement($campo106);
		$this->_form->getSubForm("detail")->addElement($campo107);
		$this->_form->getSubForm("detail")->addElement($campo109);
		$this->_form->getSubForm("detail")->addElement($campo110);
		$this->_form->getSubForm("detail")->addElement($campo111);
		$this->_form->getSubForm("detail")->addElement($campo112);
		$this->_form->getSubForm("detail")->addElement($campo114);

		// Elementi hidden non devono avere decorators
		$elements = $this->_form->getSubForm("detail")->getElements();
		foreach ($elements as $elem) {
			if ($elem instanceof Zend_Form_Element_Hidden)
				$elem->setDecorators(array('ViewHelper'));
		}
		
		// Datasources
		Zend_Loader::loadClass('MaterialCategories');
		$categorie = new MaterialCategories();
		$recordSet = $categorie->select()->from($categorie, array("id", "name"))
		->where("status > ?", 0)
		->order("name");
		$this->_view->material_categories = $categorie->fetchAll($recordSet)->toArray();
		
		
	}
	
}

